let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
    follow_index:0,
    level_index:0,
    next_follow_index:0,
    number:9,
    upImgs:[]
        
      
    },  

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        var id = options.id;
        var follow_id = options.follow_id;
        console.log(follow_id);
       // this.getCustomer(id);
        that.setData({
            platform:app.globalData.platform,    
            id:id,
            follow_id:follow_id,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain,
            uploadUrl:app.globalData.uploadUrl,
          })
    },
    goBack(){
        console.log('go back')
        wx.navigateBack(-1)
      },
      goto:function(e){
        console.log(e);
        var url = e.currentTarget.dataset.url;
        var index = e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        if(url=="#"){
    
        }else{
            if(url=='index'){
                wx.reLaunch({
                  url: url,
                })
            }else{
                wx.navigateTo({
                    url: url,
                  })
            }
        
        }
        console.log(url);
    },
      getFollowSelect(){
        let that = this;
        var open=wx.getStorageSync('open');
        var id = this.data.id
       wx.request({
         headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
         data: {
           //user_id:open.id,
           //mobile:open.phone,
           id:id
           //phone:open.phone
         
             
         },
         //method: 'POST',
         dataType: 'json',
         timeout: 30000,
         url: app.globalData.domain + 'wechat/follow/getFollowSelect',
         success: (res) => {
     
         let data = res.data;

         var level = data.level;
         var levels = [];
         levels.push('请选择');
         level.forEach(element => {
            levels.push(element.name+' '+element.desc)
         });
     
         
         console.log(data);
           that.setData({
            //follows:follows,
            follow:data.follow,
            level:levels,
            level_array:data.level
          
           })
         }
       
        })
      },
      levelChange(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          level_index:index
        })
      },
      followChange(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          follow_index:index
        })
      },
      nextFollowChange(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          next_follow_index:index
        })
      },
    upload(e){
        let that = this;
        var num = e.currentTarget.dataset.num;
        //let number
        wx.chooseMedia({
            count: num,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            camera: 'back',
            success:res=>{
                console.log(res);
                var temps = res.tempFiles;
                temps.forEach(ele=>{
                    that.uploadImg(ele.tempFilePath)
                })
                //that.uploadImg(temps);
               // console.log(temps);
            }
        })
          
    },
   uploadImg(files){
       let that = this;
       var  upImgs = this.data.upImgs;
       console.log(upImgs)
        wx.uploadFile({
        filePath: files,
        name: 'file',
        formData:{
            imageType:'follow',
            },
        url: app.globalData.domain + 'wechat/follow/upLoad',
            success:res=>{
                let data = res.data;
                data = JSON.parse(data);
                console.log(data);
                if(res.statusCode==200){
                    console.log(data.path);
                    upImgs.push(data.path);
                    that.setData({
                        upImgs:upImgs
                    })
                }
               
            }
        })
   },
   delImg(e){
       var index = e.currentTarget.dataset.index;
       let upImgs = this.data.upImgs;
       upImgs.splice(index, 1);
       this.setData({
           upImgs:upImgs
       })

   },
   onSubmit(e){
    console.log(`数据：${JSON.stringify(e.detail.value)}`)
    let that = this;
    console.log(e);
    var data= this.data;
    var value = e.detail.value;
   // value.inDate = this.data.inDate;
    var open = wx.getStorageSync('open');
    if(value.follow == 0){
      
        //console.log(stock_index);
        this.showModals('请选择本次跟进方式','#','#')
        return false;
      }
      if(value.level == 0){
      
        //console.log(stock_index);
        this.showModals('请选择本次客户级别','#','#')
        return false;
      }
      if(value.follow_content.length==0){
        this.showModals('请录入本次跟进内容','#','#')
        return false;
      }
      if(value.next_follow == 0){
      
        //console.log(stock_index);
        this.showModals('请选择下次跟进方式','#','#')
        return false;
      }
      if(value.follow_content.length==0){
        this.showModals('请录入下次跟进计划','#','#')
        return false;
      }
      let level_array = this.data.level_array;
      //let follow_array = this.data.follow_array;
      let level = level_array[value.level];
      var upImgs = this.data.upImgs;
      var id = this.data.id;

      wx.request({
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        data: {
          user_id:open.id,
          id:id,
          follow_id:data.follow_id,
          phone:open.phone,
          follow:value.follow,
          level:level,
          follow_content:value.follow_content,
          upImgs:upImgs.toString(),
          next_follow:value.next_follow,
          follow_plan:value.follow_plan     
        },
        //method: 'POST',
        dataType: 'json',
        timeout: 30000,
        url: app.globalData.domain + 'wechat/follow/follow',
        success:res=>{
            //console.log(res);
            let data = res.data;
            if(data.status==200){
                wx.showToast({
                  title: '跟进成功',
                  duration:1000,
                  success:res=>{
                      wx.navigateBack()
                  }
                })
            }else{
                wx.showToast({
                    title: '跟进失败',
                    duration:1000,
                    success:res=>{
                        wx.navigateBack()
                    }
                  })
            }
        }
      })
   },
   
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    showModals(msg,url,index){
        wx.showModal({
          title: '重要提醒',
          content: msg,
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
                if(url){
                    if(index=="index"){
                        wx.reLaunch({
                            url:url
                        })
                    }else{
                        wx.navigateTo({
                          url: url,
                        })
                    }   
                }
            }
          }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getFollowSelect();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})